/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */

/**
 * 迭代
 * @param {TreeNode} root
 * @return {number[]}
 */
var inorderTraversal1 = function (root) {
  let stack = []
  let res = []
  while (root || stack.length) {
    while (root) {
      stack.push(root)
      root = root.left
    }
    root = stack.pop()
    res.push(root.val)
    root = root.right
  }
  return res
};


/**
 * 递归
 * @param {TreeNode} root
 * @return {number[]}
 */
var inorderTraversal2 = function (root) {
  let res = []
  function inorder(root, res) {
    if (!root) return
    inorder(root.left, res)
    res.push(root.val)
    inorder(root.right, res)
  }
  inorder(root, res)
  return res
}; 